import global, { Skill } from "../../global";
import Big, { Buff, Evolution } from "../battle/big";
import Player from "../battle/player";
import Point_ts from "../battle/point";
import GameSkill from "./gameSkill";
import OtherPlayers from "./otherPlayers";
import SkillDesc from "./skillDesc";
import TouchSkill from "./touchSkill";

const {ccclass, property} = cc._decorator;

@ccclass
export default class UI extends cc.Component {

    @property(cc.Node)
    Node_bofore:cc.Node = null;     //游戏刚开给个遮挡

    @property(cc.Node)
    TurnTip:cc.Node = null;         //回合切换的时候给那么一下

    //防穿透
    @property(cc.Node)
    Layer_blockInput:cc.Node = null;

    @property(cc.Label)
    Label_action:cc.Label = null;    //行动力
    @property(cc.Label)
    Label_power:cc.Label = null;     //法力值

    @property(cc.Label)
    Label_time:cc.Label = null;     //时间
    @property(cc.Node)
    Button_turnOver:cc.Node = null; //回合结束按钮
    @property(cc.Node)
    Node_label:cc.Node = null;      //对手回合

    @property(cc.Node)
    BigAction:cc.Node = null;           //大子行动轮盘
    @property(cc.Node)
    BigTransformation:cc.Node = null;   //大子转型轮盘

    @property(cc.Node)
    Cancel:cc.Node = null;          //取消行动

    @property(cc.Node)
    Node_content:cc.Node = null;    //我的技能区域
    @property(cc.Prefab)
    Prefab_skill:cc.Prefab = null;  //技能按钮预置体

    @property(cc.Node)
    Node_otherPlayers:cc.Node = null;       //其他玩家的信息框

    skillDesc:SkillDesc = null;             //技能的信息框

    @property(cc.Node)
    Node_left:cc.Node = null;            //左侧
    
    player_ts:Player = null;        //玩家自己
    big_ts:Big = null;              //轮盘行动中需要找到是哪个big

    onLoad(){
        global.UI = this;
        //是否是复盘
        if(global.is_replay){
            this.TurnTip.active = false;
        }else{
            this.TurnTip.active = false;
            this.BigAction.active = false;
            this.BigTransformation.active = false;
            this.Layer_blockInput.active = false;
            this.Cancel.active = false;
            this.Node_label.active = false;
        }
        //图片遮挡
        this.Node_bofore.active = true;
        this.scheduleOnce(()=>{
            cc.tween(this.Node_bofore).to(1,{opacity:0}).call(()=>{this.Node_bofore.active = false;}).start();
        },1);
    }

    onDisable(){
        clearInterval(this.timer);
    }

    /**
     * 生成我的技能按钮
     */
    createSkillButton(){
        //使用技能
        let node1 = cc.instantiate(this.Prefab_skill);
        node1.getComponent(GameSkill).init(global.mgr.me_player.skill1);
        this.Node_content.addChild(node1);
        let node2 = cc.instantiate(this.Prefab_skill);
        node2.getComponent(GameSkill).init(global.mgr.me_player.skill2);
        this.Node_content.addChild(node2);
        let node3 = cc.instantiate(this.Prefab_skill);
        node3.getComponent(GameSkill).init(global.mgr.me_player.skill3);
        this.Node_content.addChild(node3);
        let node4 = cc.instantiate(this.Prefab_skill);
        node4.getComponent(GameSkill).init(global.mgr.me_player.skill4);
        this.Node_content.addChild(node4);
        //展示技能信息
        node1.getComponent(TouchSkill).skill = global.getJsonSkill(global.mgr.me_player.skill1.bh);
        node2.getComponent(TouchSkill).skill = global.getJsonSkill(global.mgr.me_player.skill2.bh);
        node3.getComponent(TouchSkill).skill = global.getJsonSkill(global.mgr.me_player.skill3.bh);
        node4.getComponent(TouchSkill).skill = global.getJsonSkill(global.mgr.me_player.skill4.bh);
    }

    

    /**
     * 点击技能描述按钮
     */
    clickSkillDescButton(){
        this.skillDesc.node.active = !this.skillDesc.node.active;
    }

    /**
     * 行动力改变
     */
    showAction(){
        if(global.is_replay){
            return;
        }
        this.Label_action.string = "行动力\n"+this.player_ts.action;
    }

    /**
     * 法力值改变
     */
    showPower(){
        if(global.is_replay){
            return;
        }
        this.Label_power.string = "法力值\n"+this.player_ts.power;
    }

    /**
     * 倒计时，只是用于显示，不会发送消息
     */
    s = 0;
    timer = null;
    timeDown(){
        clearInterval(this.timer);
        this.s = 120;
        this.timer = setInterval(()=>this.reduceTime(),1000);
    }
    reduceTime(){
        if(this.s<1){
            clearInterval(this.timer);
            //没有落小子，游戏结束
            if(!global.mgr.me_player.small){
                clearInterval(this.timer);
                global.client.send({method:"gameOver",type:"noSmall"});
                return;
            }else{
                this.turnOver();
            }
        }
        this.Label_time.string = ""+this.s;
        this.s--;
    }

    /**
     * 回合结束
     */
    turnOver(){
        if(!global.mgr.me_player.small){
            global.persist.tip("请先落小子");
            return;
        }
        global.client.send({method:"turn",id:global.id});
        this.Button_turnOver.active = false;
        this.Node_label.active = true;
    }


    /**
     * 回合切换
     */
    turnMe(){
        global.music_ts.playEffect("Audio_turnStart");
        //如果自己已经被击杀，直接回合结束
        if(global.mgr.me_player.is_surrender){
            this.turnOver();
            return;
        }
        this.openTurnTip("己 方 回 合");
        if(global.mgr.me_player.small){
            this.Cancel.active = true;
        }
        this.Node_label.active = false;
    }
    turnOthers(){
        if(global.is_replay){
            global.music_ts.playEffect("Audio_turnStart");
        }else{
            this.openTurnTip("对 手 回 合");
            this.Cancel.active = false;
            //关闭轮盘
            this.BigAction.active = false;
            this.BigTransformation.active = false;
            this.Layer_blockInput.active = false;
            this.Node_label.active = true;
        }
    }
    openTurnTip(s:string){
        this.TurnTip.stopAllActions();
        this.TurnTip.active = true;
        this.TurnTip.scale = .2;
        this.TurnTip.getComponent(cc.Label).string = s;
        cc.tween(this.TurnTip).to(.2,{scale:1}).delay(.7).call(()=>{this.TurnTip.active = false}).start();
    }

    /**
     * 取消使用轮盘
     */
    cancel(){
        //关闭轮盘
        this.closeRoulette();
        //事件归位
        global.mgr.me_player.homing();
    }

    /**
     * 关闭行动轮盘
     */
    closeRoulette(){
        cc.tween(this.BigAction).to(.2,{scale:0}).call(()=>{
            this.BigAction.active = false;
        }).start();
        cc.tween(this.BigTransformation).to(.2,{scale:0}).call(()=>{
            this.BigTransformation.active = false;
        }).start();
        this.big_ts&&this.big_ts.node&&this.big_ts.onCancel();
        //关闭防穿透
        this.Layer_blockInput.active = false;
    }

    /**
     * 出大
     */
    born(){
        //判断是否可以出大
        if(this.big_ts.arr_buff.has(Buff.fear)){
            global.persist.tip("大子怯战，只能移动");
            return;
        }
        //关闭轮盘
        this.closeRoulette();
        //开启监听
        this.big_ts.selectBorn();
    }
    
    /**
     * 移动
     */
    move(){
        //关闭轮盘
        this.closeRoulette();
        //开启监听
        this.big_ts.selectMove();
    }

    /**
     * 攻击
     */
    attack(){
        //判断是否可以攻击
        if(this.big_ts.arr_buff.has(Buff.fear)){
            global.persist.tip("大子怯战，只能移动");
            return;
        }
        //关闭轮盘
        this.closeRoulette();
        //开启监听
        this.big_ts.selectAttack();
    }

    /**
     * 转型
     */
    evolution(){
        //判断是否可以转型
        if(this.big_ts.arr_buff.has(Buff.fear)||this.big_ts.arr_buff.has(Buff.ice)||this.big_ts.arr_buff.has(Buff.virus)){
            global.persist.tip("有负面状态，不能转型");
            return;
        }
        //关闭行动轮盘
        cc.tween(this.BigAction).to(.2,{scale:0}).call(()=>{
            this.BigAction.active = false;
        }).start();
        //开启转型轮盘
        this.BigTransformation.setPosition(this.big_ts.node.parent.position);
        this.BigTransformation.active = true;
        this.BigTransformation.scale = 0;
        cc.tween(this.BigTransformation).to(.2,{scale:1.5}).start();
    }

    /**
     * 转幽灵
     */
    toGhost(){
        this.closeRoulette();
        global.client.send({method:"operation",operation:"toGhost",id:global.id,index:this.big_ts.node.parent.getComponent(Point_ts).index});
    }

    /**
     * 转顽石
     */
    toBone(){
        this.closeRoulette();
        global.client.send({method:"operation",operation:"toBone",id:global.id,index:this.big_ts.node.parent.getComponent(Point_ts).index});
    }

    /**
     * 转炮台
     */
    toBattery(){
        this.closeRoulette();
        global.client.send({method:"operation",operation:"toBattery",id:global.id,index:this.big_ts.node.parent.getComponent(Point_ts).index});
    }

    /**
     * 转地灵
     */
    toKobold(){
        this.closeRoulette();
        //检查是否已经将全部的技能解锁，否则无法转型地灵
        if(!this.checkAllSkillUnlock()){
            global.persist.tip("解锁全部技能后可用");
            this.cancel();
            return;
        }
        global.client.send({method:"operation",operation:"toKobold",id:global.id,index:this.big_ts.node.parent.getComponent(Point_ts).index});
    }

    /**
     * 检查技能是否全部解锁
     */
    checkAllSkillUnlock(){
        if(!global.mgr.me_player.skill1.is_unlock){
            return false;
        }
        if(!global.mgr.me_player.skill2.is_unlock){
            return false;
        }
        if(!global.mgr.me_player.skill3.is_unlock){
            return false;
        }
        if(!global.mgr.me_player.skill4.is_unlock){
            return false;
        }
        return true;
    }

    /**
     * 退出复盘系统
     */
    exitReplay(){
        cc.director.loadScene("index");
    }

}
